package com.example.demo.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.entity.pojo.Cart;
import com.example.demo.service.ICartService;
import com.example.demo.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 购物车表 前端控制器
 * </p>
 *
 * @author author
 * @since 2025-01-09
 */
@RestController
@RequestMapping("/cart")
public class CartController {
    @Autowired
    private ICartService cartService;

    @GetMapping("/sel")
    public R sel() {
        List<Cart>  carts = cartService.list();
        Map<String,Object> map =  new HashMap<>();
        map.put("data",carts);
        map.put("msg","查询成功");
        map.put("code",200);
        return R.ok(map);
    }
    @GetMapping("/selid")
    public R selid(long id) {
        List<Cart>  carts = cartService.list(new QueryWrapper<Cart>().eq("userid",id));
        Map<String,Object> map =  new HashMap<>();
        map.put("data",carts);
        map.put("msg","查询成功");
        map.put("code",200);
        return R.ok(map);
    }
    @PostMapping("/add")
    public R add(@RequestBody Cart cart){
        System.out.println(cart.getUserid());
        cartService.save(cart);
        Map<String,Object> map =  new HashMap<>();
        map.put("msg","添加成功");
        map.put("code",200);
        return R.ok(map);
    }
    @GetMapping("/del")
    public R del(long id){
        cartService.removeById(id);
        Map<String,Object> map =  new HashMap<>();
        map.put("msg","删除成功");
        map.put("code",200);
        return R.ok(map);
    }
    @PostMapping("/update")
    public R update(@RequestBody Cart cart){
        cartService.updateById(cart);
        Map<String,Object> map =  new HashMap<>();
        map.put("msg","修改成功");
        map.put("code",200);
        return R.ok(map);
    }
}
